package 多线程.懒汉模式;

//单例，懒汉模式
class SingletonLazy{
    private static volatile SingletonLazy instance = null;
    static Object locker = new Object();

    public static SingletonLazy getInstance(){
        if(instance == null) {
            synchronized (locker) {
                if (instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }

    private SingletonLazy(){

    }
}
public class Main {
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getInstance();
        SingletonLazy s2 = SingletonLazy.getInstance();
        System.out.println(s1 == s2);
//        SingletonLazy s3 = new SingletonLazy();//不被允许
    }
}
